# 匿名函数
from ctypes import HRESULT

fun = lambda a, b: a + b
print(fun(1,2))

# 求a中的每个元素的平方，最后求和
a = [1, 2, 3, 4]
# 方法1
for i in range(len(a)):
    a[i] = a[i] ** 2
print(a)
# 方法2  map()：映射
def f(x):
    return x ** 2
# result = map(f, a)
# 结合匿名函数
result = map(lambda x: x ** 2, a)
print(list(result))

# reduce 累积
from functools import reduce
result = reduce(lambda x, y: x + y, a)
print(result)

# filter 过滤
result = filter(lambda x: x % 2 == 0, a)
print(list(result))